package ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.visualisation.VisuControllOS;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.SettingsPanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class OsSpezPanel extends JPanel implements ActionListener, ChangeListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1085484574104691664L;
	private JCheckBox osName;
	private JCheckBox buildNr;
	private JCheckBox architektur;
	private VisuControllOS visuControllOs;
	private int amountOfFlags;

	public OsSpezPanel(VisuControllOS visuControllOs) {
		this.visuControllOs = visuControllOs;
		this.amountOfFlags = 0;
		init();
	}

	public void init() {
		this.osName = new JCheckBox("Betriebsystem-Name", this.visuControllOs.isOsName());
		this.osName.addActionListener(this);
		this.osName.addChangeListener(this);
		this.buildNr = new JCheckBox("Build-Nummer", this.visuControllOs.isBuildNr());
		this.buildNr.addActionListener(this);
		this.buildNr.addChangeListener(this);
		this.architektur = new JCheckBox("OS-Architektur", this.visuControllOs.isArchitektur());
		this.architektur.addActionListener(this);
		this.architektur.addChangeListener(this);
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(this);
		double checkBox = 0.0;
		double lable = 1.0;
		gridBagLayout.columnWeights = new double[] { checkBox, checkBox, checkBox, lable };
		int widthCheckbox = SettingsPanel.WIDTH_CHECKBOX;
		gridBagLayout.columnWidths = new int[] { widthCheckbox, widthCheckbox, widthCheckbox, 0 };
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
		int x = 0;
		int y = 0;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.osName);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.buildNr);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.architektur);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());
	}

	public void setValueToAllCheckBoxes(boolean flag) {
		this.osName.setSelected(flag);
		this.buildNr.setSelected(flag);
		this.architektur.setSelected(flag);
	}

	public void actionPerformed(ActionEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.osName) {
			this.visuControllOs.setOsName(this.osName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.buildNr) {
			this.visuControllOs.setBuildNr(this.buildNr.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.architektur) {
			this.visuControllOs.setArchitektur(this.architektur.isSelected());
			checkCanCrement();
		}
	}

	public void stateChanged(ChangeEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.osName) {
			this.visuControllOs.setOsName(this.osName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.buildNr) {
			this.visuControllOs.setBuildNr(this.buildNr.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.architektur) {
			this.visuControllOs.setArchitektur(this.architektur.isSelected());
			checkCanCrement();
		}
	}

	public void checkCanCrement() {
		if (canCrement()) {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(true);
		} else {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(false);
		}
	}

	public boolean canCrement() {
		return this.amountOfFlags < 8;
	}

	public boolean getOsNameState() {
		return this.osName.isSelected();
	}

	public boolean getBuildNrState() {
		return this.buildNr.isSelected();
	}

	public boolean getArchitekturState() {
		return this.architektur.isSelected();
	}

	public void setAllEnabledStates(boolean beEnabledFlag) {
		if (!this.osName.isSelected()) {
			this.osName.setEnabled(beEnabledFlag);
		}
		if (!this.buildNr.isSelected()) {
			this.buildNr.setEnabled(beEnabledFlag);
		}
		if (!this.architektur.isSelected()) {
			this.architektur.setEnabled(beEnabledFlag);
		}

	}
}
